/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ticketmaster.presentation.customer.purchase;

/**
 *
 * @author Wei Liu
 */
import java.awt.*;
import java.util.*;
import java.text.*;

import javax.swing.*;
import javax.swing.table.*;
import ticketmaster.presentation.controls.*;
import ticketmaster.businessobject.SeatBO;

public class SeatsTable extends JPanel
{

    private JTable table;
    private SeatTableData data;

    public SeatsTable(Collection<SeatBO> seatBOs)
    {
        setSize(572, 420);

        data = new SeatTableData(seatBOs);

        //getContentPane().add(m_title, BorderLayout.NORTH);

        table = new JTable();
        table.setAutoCreateColumnsFromModel(false);
        table.setModel(data);
        table.setPreferredScrollableViewportSize(new Dimension(500, 420));
        table.setFillsViewportHeight(true);
        table.setRowHeight(50);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.setFont(Font.getFont("Tahoma", new Font(Font.SANS_SERIF, Font.PLAIN, 14)));
        table.setShowVerticalLines(false);
//        table.setShowHorizontalLines(false);

        for (int k = 0; k < SeatTableData.columns.length; k++)
        {
            TableCellRenderer renderer;
            if (k == SeatTableData.COL_SELECTED)
            {
                renderer = new CheckCellRenderer();
            }
            else
            {
                DefaultTableCellRenderer textRenderer =
                                         new DefaultTableCellRenderer();
                textRenderer.setHorizontalAlignment(
                        SeatTableData.columns[k].getAlignment());

                renderer = textRenderer;
            }

            TableCellEditor editor;

            if (k == SeatTableData.COL_SELECTED)
            {
                JCheckBox checkBox = new JCheckBox();
                checkBox.setHorizontalAlignment(JLabel.CENTER);
                editor = new DefaultCellEditor(checkBox);
            }
            else
            {
                editor = new DefaultCellEditor(new JTextField());
            }

            TableColumn column = new TableColumn(k,
                                                 SeatTableData.columns[k].getWidth(),
                                                 renderer, editor);
            table.addColumn(column);
        }

//        for (int k = 0; k < SeatTableData.columns.length; k++)
//        {
//            DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
//            renderer.setHorizontalAlignment(
//                    SeatTableData.columns[k].m_alignment);
//            TableColumn column = new TableColumn(k,
//                                                 SeatTableData.columns[k].m_width, renderer, null);
//            table.addColumn(column);
//        }

        JTableHeader header = table.getTableHeader();
        header.setUpdateTableInRealTime(false);

        JScrollPane ps = new JScrollPane();
        ps.getViewport().add(table);
        ps.setSize(572, 420);

        this.add(ps);
    }

    /**
     * @return the table
     */
    public JTable getTable()
    {
        return table;
    }

    /**
     * @param table the table to set
     */
    public void setTable(JTable table)
    {
        this.table = table;
    }
}

class SeatTableData extends AbstractTableModel
{

    public static final int COL_SELECTED = 3;
    static final public ColumnData columns[] =
    {
        new ColumnData("Seat level", 150, JLabel.CENTER),
        new ColumnData("Seat number", 150, JLabel.CENTER),
        new ColumnData("Price", 150, JLabel.CENTER),
        new ColumnData("Selected", 150, JLabel.CENTER)
    };
    protected SimpleDateFormat frm;
    private Vector vector;

    public SeatTableData(Collection<SeatBO> seatBOs)
    {
        frm = new SimpleDateFormat("MM/dd/yyyy");
        vector = new Vector();
        setData(seatBOs);
    }

    public void setData(Collection<SeatBO> seatBOs)
    {
        vector.removeAllElements();
        if (seatBOs != null)
        {
            vector.addAll(seatBOs);
        }
    }

    public int getRowCount()
    {
        return vector == null ? 0 : vector.size();
    }

    public int getColumnCount()
    {
        return columns.length;
    }

    public String getColumnName(int column)
    {
        return columns[column].getTitle();
    }

    public Class getColumnClass(int c)
    {
        return getValueAt(0, c).getClass();
    }

    public boolean isCellEditable(int nRow, int nCol)
    {
        if (nCol < 3)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    public Object getValueAt(int nRow, int nCol)
    {
        if (nRow < 0 || nRow >= getRowCount())
        {
            return "";
        }
        SeatBO row = (SeatBO) vector.elementAt(nRow);
        switch (nCol)
        {
            case 0:
                return row.getLevel();
            case 1:
                return row;
            case 2:
                return row.getPrice();
            case 3:
                return row.isSelected();
        }
        return "";
    }

    public void setValueAt(Object value, int nRow, int nCol)
    {
        if (nRow < 0 || nRow >= getRowCount())
        {
            return;
        }
        SeatBO row = (SeatBO) vector.elementAt(nRow);
        //String svalue = value.toString();

        switch (nCol)
        {
            case COL_SELECTED:
                row.setSelected((Boolean) value);
                break;
        }
    }

    public String getTitle()
    {
        return "Available seats";
    }
}

